package com.df.my_net_game.net.tcp;

import com.df.my_net_game.game_rpc.GRpcHeader;
import com.df.my_net_game.game_rpc.EzGameMsg;
import com.df.my_net_game.net.base.GTcpServer;
import com.df.my_net_game.net.base.TcpServerCB;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.timeout.IdleStateEvent;

public class EzGameTcpDefaultServiceCb extends TcpServerCB<EzGameMsg> {



    @Override
    public void onServerStartSuccess(ChannelFuture future, GTcpServer server) {

    }

    @Override
    public void onServerStartFailure(ChannelFuture future, GTcpServer server) {

    }

    @Override
    public void onException(ChannelHandlerContext ctx, Throwable cause, GTcpServer server) {

    }

    @Override
    public EzGameMsg onReceiveMsg(EzGameMsg msg, GTcpServer server) {
        return msg;
    }

    @Override
    public EzGameMsg onBeforeSendMsg(EzGameMsg respMsg, GTcpServer server) {
        return respMsg;
    }

//    @Override
//    public EzGameMsg decode(ByteBuf byteBuf, GTcpServer server) {
//        if(byteBuf.readByte()<4){
//            return null;
//        }
//        byteBuf.markReaderIndex();
//        int len = byteBuf.readInt();
//        if(byteBuf.readableBytes() < len-4){
//            byteBuf.resetReaderIndex();
//            return null;
//        }
//        //todo 循环读一个消息长度进行解析
//        EzGameMsg msg = new EzGameMsg();
//        GRpcHeader header = new GRpcHeader();
//
//        msg.setHeader(header);
//        byte[] headerBytes = new byte[header.lenth()];
//        byteBuf.readBytes(headerBytes);
//        header.readFromBytes(headerBytes);
//
//        byte[] bodyBytes = new byte[len-4-header.lenth()];
//        msg.setBody(bodyBytes);
//
//        return msg;
//    }
//
//    @Override
//    public  ByteBuf encode(EzGameMsg respMsg, GTcpServer server) {
//        ByteBuf byteBuf = respMsg.getChannel().alloc().heapBuffer();
//        byteBuf.writeInt(respMsg.length())
//                .writeBytes(respMsg.getHeader().tobytes())
//                .writeBytes(respMsg.getBody());
//        return byteBuf;
//    }

    @Override
    public void onDIdle(ChannelHandlerContext ctx, IdleStateEvent event, GTcpServer server) {

    }
}
